<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="auditForm" :model="auditData" labelWidth="100px">
				<!-- 上报信息展示 -->
				<uni-forms-item label="上报员工" name="employeeName">
					<uni-easyinput v-model="auditData.employeeName" placeholder="员工姓名" disabled />
				</uni-forms-item>

				<uni-forms-item label="上报日期" name="reportDate">
					<uni-easyinput v-model="auditData.reportDate" placeholder="上报日期" disabled />
				</uni-forms-item>

				<uni-forms-item label="上报金额" name="reportedAmount">
					<uni-easyinput v-model="auditData.reportedAmount" placeholder="上报现金金额" suffixIcon="currency"
						disabled />
				</uni-forms-item>

				<!-- 审核信息填写 -->
				<uni-forms-item label="实际金额" name="actualAmount" required>
					<uni-easyinput v-model="auditData.actualAmount" placeholder="请输入实际金额" type="number"
						suffixIcon="money" :clearable="false" />
				</uni-forms-item>

				<uni-forms-item label="审核状态" name="auditStatus" required>
					<uni-data-checkbox v-model="auditData.auditStatus" :localdata="auditOptions" mode="button" />
				</uni-forms-item>

				<uni-forms-item label="差额" name="difference">
					<uni-easyinput v-model="auditData.difference" placeholder="自动计算" disabled
						:style="{ color: auditData.difference < 0 ? '#dd524d' : '#4cd964' }" />
				</uni-forms-item>

				<uni-forms-item label="审核备注" name="auditNotes">
					<uni-easyinput v-model="auditData.auditNotes" placeholder="请输入审核意见（如差额原因）" type="textarea"
						:maxlength="200" />
				</uni-forms-item>
			</uni-forms>

			<button type="primary" @click="handleAuditSubmit" :loading="submitting">提交审核</button>
		</view>
	</view>
</template>

<script>
	import {
		getReports,
		auditCashReport
	} from "@/api/system/cash/reports"
	export default {
		data() {
			return {
				submitting: false,
				auditData: {},
				reportId: '',
				auditOptions: [{
						value: 'APPROVED',
						text: '审核通过'
					},
					{
						value: 'REJECTED',
						text: '审核驳回'
					}
				]
			}
		},
		onLoad(options) {
			getReports(options.id).then(response => {
				console.log(response)
				this.auditData = response.data
			})
			this.reportId = options.id;
		},
		onReady: function() {

		},
		watch: {
			'auditData.actualAmount'(newVal) {
				if (newVal && !isNaN(newVal)) {
					this.auditData.difference = Number(newVal) - this.auditData.reportedAmount
				}
			}
		},
		methods: {
			async handleAuditSubmit() {
				try {
					this.submitting = true
					const valid = await this.$refs.auditForm.validate()

					if (valid) {
						// 提交审核逻辑
						const params = {
							reportId: this.reportId, // 实际应来自页面参数
							actualAmount: this.auditData.actualAmount,
							auditStatus: this.auditData.auditStatus,
							auditNotes: this.auditData.auditNotes,
							// difference: this.auditData.difference,
							auditorId: this.$store.state.user.id
						}

						// 调用审核接口
						auditCashReport(params).then(response => {
							if(response.code === 200){
								uni.showToast({
									title: '审核提交成功',
									icon: 'success'
								})
								
								setTimeout(() => {
									this.$tab.navigateTo('/pages/work/cash/auditlist')
								}, 1500)
							}else{
								uni.showToast({
									title: '审核提交失败',
									icon: 'error'
								})
								
								setTimeout(() => {
									uni.navigateBack()
								}, 1500)
							}
							
							
						})
					}
				} catch (error) {
					console.error('审核提交失败:', error)
				} finally {
					this.submitting = false
				}
			}
		}
	}
</script>

<style scoped>
	.container {
		padding: 20rpx;
	}

	.example {
		background: #fff;
		padding: 30rpx;
		border-radius: 16rpx;
		box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
	}
</style>